package hu.lacimol.tutorial.webwork;

import java.util.Map;
import java.util.TreeMap;

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.UserUtils;
import com.atlassian.jira.web.action.JiraWebActionSupport;

public class HelloAction extends JiraWebActionSupport {

	private static final long serialVersionUID = 6545090412954507661L;

	private String userName;

	@Override
	protected String doExecute() throws Exception {
		log.debug("Entering doExecute");
		String loggedUserName = getLoggedUserName();
		User admin = UserUtils.getUser("admin");
		userName = loggedUserName != null ? loggedUserName : admin.getDisplayName();
		return SUCCESS;
	}

	public String getUserName() {
		return userName;
	}

	public String getLoggedUserName() {
		User loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
		return loggedInUser == null ? null : loggedInUser.getDisplayName();
	}

	public Map<String, Object> getCtx() {
		// rendezett velocity parameterek
		return new TreeMap<String, Object>(ComponentAccessor.getVelocityParamFactory().getDefaultVelocityParams());
	}

}